package com.wkhyapp.lm.adapter;

import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.utils.GetTimeAgo;
import com.wkhyapp.lm.utils.ImageLoadUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2021-10-6.
 */

public class GoodsBatchListAdapter extends BaseQuickAdapter<Goods, BaseViewHolder> {


    // 数据集
    private List<Goods> list = new ArrayList<Goods>();
    // 上下文
    // 存储勾选框状态的map集合
    private Map<Integer, Boolean> isCheck = new HashMap<Integer, Boolean>();

    public GoodsBatchListAdapter(int layoutResId, List<Goods> data) {
        super(layoutResId, data);
        initCheck(false);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final Goods item) {

        ImageView imageView = helper.getView(R.id.goods_iv);
        helper.setText(R.id.imgCount_tv,item.getImgCount()+"");
        ImageLoadUtil.setRoundImage_Normal(mContext,item.getImg(),10,imageView);
        helper.setText(R.id.time_tv, GetTimeAgo.getTimeAgo(item.getUpdateTime()));
        helper.setText(R.id.title_tv,item.getDesc());
        CheckBox share_cb = helper.getView(R.id.chb_select_way_point);


        share_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheck.put(helper.getPosition(), isChecked);
            }
        });
        // 设置状态
        if (isCheck.get(helper.getPosition()) == null) {
            isCheck.put(helper.getPosition(), false);
        }
        share_cb.setChecked(isCheck.get(helper.getPosition()));

    }

    // 初始化map集合
    public void initCheck(boolean flag) {
        // map集合的数量和list的数量是一致的
        for (int i = 0; i < list.size(); i++) {
            // 设置默认的显示
            if(!list.get(i).isExist()){
                isCheck.put(i, flag);
            }

        }
    }
    // 设置数据
    public void setData(List<Goods> data) {
        this.list = data;
    }

    // 全选button获取状态
    public Map<Integer, Boolean> getMap() {
        // 返回状态
        return isCheck;
    }

}
